package com.sunshine.controller;

import com.google.common.base.Preconditions;
import com.mysql.cj.util.StringUtils;
import com.sunshine.common.entity.Result;
import com.sunshine.controller.convert.SubjectLabelConvertDTO;
import com.sunshine.controller.dto.SubjectLabelDTO;
import com.sunshine.domain.entity.SubjectLabelBO;
import com.sunshine.domain.service.SubjectLabelDomainService;
import com.sunshine.infra.basic.entity.SubjectMapping;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @description: 题目标签控制层
 * @Author 夏百友
 * @Date 2025/11/5 17:33
 */
@Slf4j
@RestController
@RequestMapping("/label")
public class SubjectLabelController {

    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    /**
     * @description: 添加标签
     * @author 夏百友
     * @date 2025/11/6 16:36
     */
    @PostMapping("/add")
    public Result<Boolean> addSubjectCategory(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("controller.SubjectLabelController.addSubjectCategory:参数{}", subjectLabelDTO);
            }

            Preconditions.checkArgument(!StringUtils.isNullOrEmpty(subjectLabelDTO.getLabelName()), "标签名称不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelConvertDTO.INSTANCE.convertLabelDtoToBo(subjectLabelDTO);
            Boolean add = subjectLabelDomainService.add(subjectLabelBO);
            log.info("controller.SubjectLabelController.addSubjectCategory:结果{}", add);
            return Result.ok(add);
        } catch (Exception e) {
            log.error("controller.SubjectLabelController.addSubjectCategory:异常 {}", e.getMessage(), e);
            return Result.fail();
        }
    }

    /**
     * @description: 修改标签
     * @author 夏百友
     * @date 2025/11/6 16:36
     */

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("controller.SubjectLabelController.update:参数{}", subjectLabelDTO);
            }
            Preconditions.checkArgument(subjectLabelDTO.getId() != null, "标签id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelConvertDTO.INSTANCE.convertLabelDtoToBo(subjectLabelDTO);
            Boolean update = subjectLabelDomainService.update(subjectLabelBO);
            log.info("controller.SubjectLabelController.update:结果{}", update);
            return Result.ok(update);
        }catch (Exception e){
            log.error("controller.SubjectLabelController.update:异常 {}", e.getMessage(), e);
            return Result.fail("更新标签失败");
        }
    }

    /**
     * @description: 删除标签
     * @author 夏百友
     * @date 2025/11/6 16:36
     */

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("controller.SubjectLabelController.delete:参数{}", subjectLabelDTO);
            }
            Preconditions.checkArgument(subjectLabelDTO.getId() != null, "标签id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelConvertDTO.INSTANCE.convertLabelDtoToBo(subjectLabelDTO);
            Boolean delete = subjectLabelDomainService.delete(subjectLabelBO);
            log.info("controller.SubjectLabelController.delete:结果{}", delete);
            return Result.ok(delete);
        }catch (Exception e){
            log.error("controller.SubjectLabelController.delete:异常 {}", e.getMessage(), e);
            return Result.fail("删除标签失败");
        }
    }

    /**
     * @description: 查询分类下的标签
     * @author 夏百友
     * @date 2025/11/6 17:29
     */

    @PostMapping("/queryLabelByCategroyId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategroyId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("controller.SubjectLabelController.queryLabelByCategroyId:参数{}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelConvertDTO.INSTANCE.convertLabelDtoToBo(subjectLabelDTO);
            List<SubjectLabelBO> queryLabelByCategroyId = subjectLabelDomainService.queryLabelByCategroyId(subjectLabelBO);
            List<SubjectLabelDTO> subjectLabelDTOS = SubjectLabelConvertDTO.INSTANCE.convertLabelBoToDtoList(queryLabelByCategroyId);
            log.info("controller.SubjectLabelController.queryLabelByCategroyId:结果{}", subjectLabelDTOS);
            return Result.ok(subjectLabelDTOS);
        }catch (Exception e){
            log.error("controller.SubjectLabelController.queryLabelByCategroyId:异常 {}", e.getMessage(), e);
            return Result.fail("查询标签失败");
        }
    }





}
